﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using black_screen_client.Models;
using black_screen_client.Utils;
using Microsoft.Extensions.Configuration;

namespace black_screen_client
{
    public static class ProjectTools
    {
        private static IConfiguration configuration = new ConfigurationBuilder().SetBasePath(Environment.CurrentDirectory).AddJsonFile("appsettings.json", false, true).Build();

        private static string iniFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config", "sysConfig.ini");
        private static IniFile iniFile = new IniFile(iniFilePath);

        public static TcpServerModel getTcpServer()
        {
            TcpServerModel tcpServerModel = new TcpServerModel();
            int portNumber = 0;
            int.TryParse(iniFile.Read("tcpServer", "portNumber"), out portNumber);

            int clientId = 0;
            int.TryParse(iniFile.Read("tcpServer", "clientId"), out clientId);
            tcpServerModel.IpAddress = iniFile.Read("tcpServer", "ipAddress");
            tcpServerModel.PortNumber = portNumber;
            tcpServerModel.ClientId = clientId;
            return tcpServerModel;
        }
        public static string configDbConnString
        {
            get
            {
                return iniFile.Read("configDbConnection", "connString");

            }
        }

        /// <summary>
        /// 根据PHD 中的 时间信息字符串获取格林威治时间
        /// </summary>
        /// <param name="timesamp"></param>
        /// <returns></returns>
        public static bool ConvertActiveTime(string timesamp, out DateTime actime)
        {
            bool rtn = false;
            actime = default(DateTime);
            try
            {
                string ticks = timesamp.Substring(timesamp.Length - 7, 7);
                string second = timesamp.Substring(0, timesamp.Length - 7);

                // 格林威治时间 1601/1/1
                DateTimeOffset basetime = new DateTimeOffset(1601, 1, 1, 0, 0, 0, TimeSpan.Zero);
                DateTimeOffset timeOffset = basetime.AddSeconds(double.Parse(second));
                actime = timeOffset.DateTime + new TimeSpan(long.Parse(ticks));
                rtn = true;
            }
            catch (Exception ex) { }
            return rtn;
        }

        public static string GenBsGroupInfo(string group)
        {
            return String.Concat(group, " 单元已进入黑屏状态");
        }

        public static void ShowScreenInfo()
        {
            var screens = System.Windows.Forms.Screen.AllScreens;
            for (int i = 0;i < screens.Length; i++)
            {
                Logger.Singleton.Info($"屏幕信息：屏幕索引：{i}--屏幕名称：{screens[i].DeviceName}--屏幕分辨率：{screens[i].Bounds.Width}x{screens[i].Bounds.Height}--工作区域：{screens[i].WorkingArea.Width}x{screens[i].WorkingArea.Height}");
            }
        }

        public static System.Windows.Forms.Screen GetScreen(int requestedScreen)
        {
            var screens = System.Windows.Forms.Screen.AllScreens;
            var mainScreen = 0;
            if (screens.Length > 1 && mainScreen < screens.Length && requestedScreen<screens.Length)
            {
                return screens[requestedScreen];
            }
            return screens[0];
        }

      
        public static void ShowWindow(Window window,  ScreenPosition position)
        {
            int requestedScreen = Convert.ToInt32(iniFile.Read("DisplayScreenInfo","screenIndex"));
            var screen = GetScreen(requestedScreen);
         
            window.WindowState = WindowState.Normal;
            window.WindowStartupLocation = WindowStartupLocation.Manual;
            switch(position)
            {
                case ScreenPosition.LeftUp:
                    window.Left = screen.WorkingArea.Left + 100;
                    window.Top = screen.WorkingArea.Top + 100;
                    break;
                case ScreenPosition.RightUp:
                    window.Left = screen.WorkingArea.Right - window.Width-100;
                    window.Top = screen.WorkingArea.Top+100;
                    break;
                case ScreenPosition.LeftDown:
                    window.Left = screen.WorkingArea.Left + 100;
                    window.Top = screen.WorkingArea.Bottom - window.Height - 100;
                    break;
                case ScreenPosition.RightDown:
                    window.Left = screen.WorkingArea.Right - window.Width - 100;
                    window.Top = screen.WorkingArea.Bottom - window.Height -100;
                    break;
                case ScreenPosition.Center:
                    window.Left = screen.WorkingArea.Left + (screen.WorkingArea.Width - window.Width) / 2;
                    window.Top = screen.WorkingArea.Top + (screen.WorkingArea.Height - window.Height) / 2;
                    break;
                default:
                    window.Left = screen.WorkingArea.Left + 100;
                    window.Top = screen.WorkingArea.Top + 100;
                    break;
            }
            //Logger.Singleton.Info($"屏幕显示位置：left:{window.Left}--top:{window.Top} ");
            window.Show();
        }

        public static void InsertLogInfo(string description, string type)
        {
            string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            SqlHelper sqlHelper = new SqlHelper(configDbConnString);
            string sql = string.Format("insert into log(description,type,time) values('{0}','{1}','{2}')", description, type, time);
            sqlHelper.ExecuteNonQuery(sql);

        }
    }
}
